﻿using System;
using System.Windows;
using System.Windows.Controls;
using QDP.Platform.Services;
using QDP.Platform.Silverlight;

namespace Apps.Common
{
    [WidgetMeta(Key = KEY_TEXTFORMAT
        , Desc = "表示应用显示的文本格式。"
        , DefaultValue = DEF_TEXT_FORMAT)]
    public class UserInfoWidget : PartElement
    {
        const string KEY_TEXTFORMAT = "textformat";
        const string DEF_TEXT_FORMAT = "欢迎您，username,（今天是yyyy年MM月dd日week）";
        private TextBlock TextView;
        public string Week()
        {
            string[] weekdays = { "星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六" };
            string week = weekdays[Convert.ToInt32(DateTime.Now.DayOfWeek)];
            return week;
        }
        public override void Show()
        {
            string format = this.GetConfigValue(KEY_TEXTFORMAT);
            if (string.IsNullOrEmpty(format)) format = DEF_TEXT_FORMAT;

            var user = Q.DataManager.GetUserData() as Identity;
            string text = format;
            text = text.Replace("username", user.Caption);
            text = text.Replace("teamname", user.TeamCaption);
            text = text.Replace("week", Week());
            text = DateTime.Now.ToString(text);
            TextView = new TextBlock()
            {
                Margin = new Thickness(10, 0, 0, 0),
                Text = text,
                VerticalAlignment = VerticalAlignment.Center
            };
            Q.ToolbarManager.Add(TextView, ToolbarLayoutDirection.Custom);
            IsShowed = true;
        }
        public override void Close()
        {
            IsShowed = false;
            Q.ToolbarManager.Remove(TextView);
        }
    }
}